<?php
/**
 * author      :chenwenwen
 * createTime  :2020-04-06 14:59
 * description :
 */

namespace app\common\model\mysql;

use think\Model;

class User extends Model {

    /**
     * 自动生成 写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 根据手机号获取用户数据
     */
    public function getUserByPhoneNumber($phoneNumber) {
        if (empty($phoneNumber)) return false;
        $where = [
            'phone_number' => trim($phoneNumber)
        ];
        $result = $this->where($where)->find();
        return $result;

    }

    /**
     * 通过ID获取用户数据
     * @param $id
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserById($id) {
        $id = intval($id);
        if (!$id) return false;
        return $this->find($id);
    }

    /**
     * 根据主键ID更新数据表数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id, $data) {
        $id = intval($id);
        if ($id <= 0 || empty($data) || !is_array($data)) return false;

        $where = ['id' => $id];
        return $this->where($where)->save($data);

    }

    /**
     * 通过用户名获取用户数据
     * @param $id
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByUsername($username) {
        if (!$username) return false;
        return $this->where(['username' => $username])->find();
    }


}